import {WeaLocaleProvider} from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

export const menuData = store => [{
    command: store['edc.excelView.tableInsertCombineRow'],
    text: getLabel('503000','插入合并行'),
    name: "edc.excelView.tableInsertCombineRow",
    workArea: "viewporttable",
}, {
    command: store['edc.excelView.tableInsertTopRow'],
    text: getLabel('502989','在上方插入表行'),
    name: "edc.excelView.tableInsertTopRow",
    workArea: "viewporttable",
}, {
    command: store['edc.excelView.tableInsertBottomRow'],
    text: getLabel('502990','在下方插入表行'),
    name: "edc.excelView.tableInsertBottomRow",
    workArea: "viewporttable",
}, {
    command: store['edc.excelView.custableInsertRightCol'],
    text: getLabel('511616','在右边插入表列') ,
    name: "edc.excelView.custableInsertRightCol",
    workArea: "viewporttable",
}, {
    command: store['edc.excelView.custableInsertLeftCol'],
    text: getLabel('511617','在左边插入表列') ,
    name: "edc.excelView.custableInsertLeftCol",
    workArea: "viewporttable",
}, {
    command: store['edc.excelView.tableDeleteRow'],
    text: getLabel('502992','删除表行'),
    name: "edc.excelView.tableDeleteRow",
    workArea: "viewporttable",
}, {
    command: store['edc.excelView.custableDeleteCol'],
    text: getLabel('511618','删除表列') ,
    name: "edc.excelView.custableDeleteCol",
    workArea: "viewporttable",
}]
export const setRightMenu = (store, isTable, isFirstDataRow, table, cellType, rowControl,sheet) => {
    const datas = menuData(store);
    if (isTable !== "0") {
        
        let isAddRow = 1;
        let isDeleteRow = 1;
        const bindingPath = table.bindingPath();
        if(!bindingPath) {

            if(table.name().startsWith("custom")) {
                delete datas[0];
                const {row, col, rowCount, colCount} = table.dataRange();

                let hasRightExpand = false;
                let hasDownExpand = false;
                for(let r = row ; r < row + rowCount; r++) {
                    for(let c = col ; c < col + colCount; c++) {
                        const cell = sheet.getCell(r, c);
                        const cType = cell.cellType();
                        if(cType) {
                            if(cType.expandType == '2') {
                                hasDownExpand = true;
                            } else if(cType.expandType == '1') {
                                hasRightExpand = true;
                            }
                        }
                        if(hasRightExpand && hasDownExpand) {
                            break;
                        }
                    }
                    if(hasRightExpand && hasDownExpand) {
                        break;
                    }
                }
                if(!hasRightExpand && !hasDownExpand) {
                    store.workbook.contextMenu.menuData = [];
                    return;
                } else if(!hasRightExpand && hasDownExpand) {
                    delete datas[3];
                    delete datas[4];
                    delete datas[6];
                } else if(hasRightExpand && !hasDownExpand) {
                    delete datas[1];
                    delete datas[2];
                    delete datas[5];
                }
                if(cellType) {
                    const expandType = cellType.expandType;
                    if(expandType == "1") {
                        delete datas[1];
                        delete datas[2];
                        delete datas[3];
                        delete datas[4];
                        delete datas[5];
                    } else if(expandType == "2") {
                        delete datas[1];
                        delete datas[2];
                        delete datas[3];
                        delete datas[4];
                        delete datas[6];
                    }
                } else {
                    store.workbook.contextMenu.menuData = [];
                    return;
                }
            } else {
                store.workbook.contextMenu.menuData = [];
                return;
            }

        } else {
            delete datas[3];
            delete datas[4];
            delete datas[6];
            const sheetName = sheet.name();
            (rowControl[sheetName]||[]).forEach(item => {
                if(bindingPath == item.bindingPath){
                    isAddRow = item.isAddRow;
                    isDeleteRow = item.isDeleteRow;
                    return;
                }
            });
        }
		
        if(0 == isAddRow){
            delete datas[0];
            delete datas[1];
            delete datas[2];
            // datas.splice(0,3);
        } 
        if(0 == isDeleteRow){
            delete datas[5];
            // datas.splice(3,1);
        }
        
        if(!cellType){
            delete datas[0];
            delete datas[1];
            delete datas[5];
        }

        doCodeFunctions(store,bindingPath,datas);

        if (isFirstDataRow) {
            store.workbook.contextMenu.menuData = datas.filter(d => {
                if (d.subMenu) {
                    d.subMenu = d.subMenu.filter(f => {
                        return /table/i.test(f.workArea) && !(/nofirstdatarow/i.test(f.workArea))
                    });
                }
                return /table/i.test(d.workArea) && !(/nofirstdatarow/i.test(d.workArea))
            });
        } else {
            store.workbook.contextMenu.menuData = datas.filter(d => /table/i.test(d.workArea))
        }
    } else {
        store.workbook.contextMenu.menuData = datas.filter(d => d.workArea !== 'viewporttable');
    }
}

export const doCodeFunctions = (store,bindingPath,datas) => {
    if(store.hideInsertSpanRow) {
        store.hideInsertSpanRow.some(path=>{
            if(bindingPath && bindingPath.replace("_","")==path){
                delete datas[0];
            }else if('all'==path){
                delete datas[0];
                return true;
            }
        })
    }
    if(store.hideInsertTopRow) {
        store.hideInsertTopRow.some(path=>{
            if(bindingPath && bindingPath.replace("_","")==path){
                delete datas[1];
            }else if('all'==path){
                delete datas[1];
                return true;
            }
        })
    }
    if(store.hideInsertBottomRow) {
        store.hideInsertBottomRow.some(path=>{
            if(bindingPath && bindingPath.replace("_","")==path){
                delete datas[2];
            }else if('all'==path){
                delete datas[2];
                return true;
            }
        })
    }
    if(store.hideDeleteRow) {
       store.hideDeleteRow.some(path=>{
            if(bindingPath && bindingPath.replace("_","")==path){
                delete datas[5];
            }else if('all'==path){
                delete datas[5];
                return true;
            }
        })
    }
}

